/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import clases.*;
import clases.Conexion;
import javax.servlet.http.HttpSession;

/**
 *
 * @author ad
 */
public class EditarCliente extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
          //por si entra sin loguearse al serlet
        HttpSession sesionOk = request.getSession();
            if (sesionOk.getAttribute("Usuario") == null) {
            response.setContentType("text/html;charset=UTF-8");
            response.sendRedirect("/ProyectoBases2/index.jsp");
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
        String nombre1=request.getParameter("editar_nombre_actual");//obtenenos el resultado
        Conexion mi=new Conexion();
        String res[]=mi.regresarCliente_editar(nombre1);

        //agregamos el id modificado del cliente en cuestion solo para eso lo usamos
        HttpSession sesionOk = request.getSession();
        sesionOk.setAttribute("id_modificar",res[2]);

        //esto imprime la lista que se va a mostrar de cliente en la aplicacion
        responderPagina(res[0]+res[1], response,"Inicio","Editar_Cliente.jsp");

       

    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>



    public void responderPagina(String mensaje, HttpServletResponse response, String Ir_a,String pagina) throws IOException {
       PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
             */
           out.println(
            "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
            +"<html xmlns=\"http://www.w3.org/1999/xhtml\">"
            +"<head>"
            +"<title>Crear Nuevo Usuario</title>"
            +"<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />"
            +"<link href=\"style.css\" rel=\"stylesheet\" type=\"text/css\" />"
            +"<script type=\"text/javascript\" src=\"js/jquery-1.3.2.min.js\"></script>"
            +"<script type=\"text/javascript\" src=\"js/script.js\"></script>"
            +"<script type=\"text/javascript\" src=\"js/cufon-yui.js\"></script>"
            +"<script type=\"text/javascript\" src=\"js/arial.js\"></script>"
            +"<script type=\"text/javascript\" src=\"js/cuf_run.js\"></script>"
            +"<meta http-equiv==\"Pragma\" content=\"no-cache\"/>"
            +"<meta http-equiv=\"Expires\" content=\"-1\"/>"
            +"<meta http-equiv==\"Cache-Control\" content=\"no-store\"/>"
            +"<meta http-equiv=\"Cache\" content=\"no-cache\"/>"
            +"<script lenguaje=\"JavaScript\">"
            +"if(window.history.forward(1) != null)"
            +"    window.history.forward(1);"
            +"</script>"
            +"</head>"
            +"<body>"
            +"<div class=\"main\">"
              +"<div class=\"header\">"
                +"<div class=\"header_resize\">"
                  +"<div class=\"logo\">"
                    +"<h1><a href=\"#\"><span>Siempre</span> a su lado</a></h1>"
                  +"</div>"
                  +"<div class=\"menu_nav\">"
                    +"<ul>"
                     +" <li> <a href=\""+pagina+"\">Regresar</a></li>"
                     +"</ul>"
                  +"</div>"
                  +"<div class=\"clr\"></div>"
                  +"<div class=\"hbg\"><img src=\"images/header_images.jpg\" width=\"400\" height=\"275\" alt=\"\" class=\"fl\" />"
                    +"<div class=\"info fl\">"
                      +"<h3>Banco Central <br />"
                        +"Uniendo al mundo </h3>"
                    +"</div>"
                    +"<div class=\"clr\"></div>"
                  +"</div>"
                  +"<div class=\"clr\"></div>"
                +"</div>"
              +"</div>"
              +"<div class=\"content\">"
                +"<div class=\"content_resize\">"
                  +"<div class=\"mainbar\">"
                    +"<div class=\"article\">"
                      +"<h2><span>Actualizando Datos</span></h2>"
                      +"<div class=\"clr\"></div>"
                     +"</div>"
                    +"<div class=\"article\">"
                     +"<div class=\"clr\"></div>"
                      +"<form action=\""+Conexion.localhost+"EditarCliente_aux\" method=\"post\">"
                        +"<ol>"
                            +mensaje
                          +"<br>"
                          +"<li>"
                           +"<input type=\"submit\" value=\"Actualizar\" align=\"center\" ><br>"
                            +"<div class=\"clr\"></div>"
                          +"</li>"
                        +"</ol>"

                     +" </form>"
                  +"</div>"
                  +"</div>"
                  +"<div class=\"sidebar\">"
                    +"<div class=\"gadget\">"
                      +"<h2 class=\"star\"><span>Nuestro</span> Menu</h2>"
                      +"<div class=\"clr\"></div>"
                      +"<ul class=\"sb_menu\">"
                        +"<li><a href=\""+pagina+"\">Inicio</a></li>"

                     +" </ul>"
                    +"</div>"

                  +"</div>"
                 +" <div class=\"clr\"></div>"
                +"</div>"
              +"</div>"

            +"  <div class=\"footer\">"
                +"<div class=\"footer_resize\">"
                +"  <p class=\"lf\">&copy; Copyright <a href=\"#\">MyWebSite</a>.</p>"
                  +"<p class=\"rf\">Layout by I <a href=\"http://www.iwebsitetemplate.com/\">Website Templates</a></p>"
                 +" <div class=\"clr\"></div>"
               +" </div>"
              +"</div>"
          +"  </div>"
          +"  </body>"
          +"  </html>");
        }catch(Exception xs)
        {
            System.out.println("***********generado en la clase ERROR_DATA metodo EscribirMansaje 4 parametros");
            System.out.println(xs.getMessage());
        }
        finally {
            out.close();
        }
    }

}
